package com.imddysc.springbootbatch001.utils;

import java.text.ParseException;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerKey;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

public class JobManager {

	// 定时任务工厂
	public static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
	public static Scheduler sched;
	// 任务组名称
	private static String JOB_GROUP_NAME = "TIMED_TASK";
	// 触发器组名称
	private static String TRIGGER_GROUP_NAME = "TRIGGER_TIMED_TASK";

	/**
	 * addJob方法慨述:添加一个定时任务，使用默认的任务组名，触发器名，触发器组名 <br>
	 * 
	 * @param jobName  任务名 <br>
	 * @param jobClass 任务类名 <br>
	 * @param time     时间设置，参考quartz说明文档
	 * 
	 * @throws ParseException
	 */
	@SuppressWarnings("unchecked")
	public static void addJob(String jobName, String jobClass, String time) throws ParseException {
		try {
			sched = gSchedulerFactory.getScheduler();
			// 任务名，任务组，任务执行类
			JobDetailImpl jobDetail = new JobDetailImpl();
			jobDetail.setName(jobName);
			jobDetail.setGroup(JOB_GROUP_NAME);
			jobDetail.setJobClass((Class<? extends Job>) Class.forName(jobClass));
			// 定义时间触发器
			CronTriggerImpl cronTrigger = new CronTriggerImpl();
			cronTrigger.setName(jobName);
			cronTrigger.setGroup(TRIGGER_GROUP_NAME);
			cronTrigger.setCronExpression(time);

			sched.scheduleJob(jobDetail, cronTrigger);
			// 启动
			// if (!sched.isShutdown()){
			// sched.start();
			// }
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 修改一个任务的触发时间(使用默认的任务组名，触发器名，触发器组名)
	 * 
	 * @param jobName
	 * @param time
	 */
	@SuppressWarnings("rawtypes")
	public static void modifyJobTime(String jobName, String time) {
		try {
			sched = gSchedulerFactory.getScheduler();
			CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME));
			if (trigger == null) {
				return;
			}
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, JOB_GROUP_NAME));
				Class objJobClass = jobDetail.getJobClass();
				String jobClass = objJobClass.getName();
				removeJob(jobName);

				addJob(jobName, jobClass, time);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 移除一个任务(使用默认的任务组名，触发器名，触发器组名)
	 * 
	 * @param jobName
	 */
	public static void removeJob(String jobName) {
		try {
			sched = gSchedulerFactory.getScheduler();
			sched.pauseTrigger(TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME)); // 停止触发器
			sched.unscheduleJob(TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME)); // 移除触发器
			sched.deleteJob(JobKey.jobKey(jobName, JOB_GROUP_NAME)); // 删除任务
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 启动所有定时任务
	 */
	public static void startJobs() {
		try {
			sched.start();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 关闭所有定时任务
	 */
	public static void shutdownJobs() {
		try {
			if (sched != null) {
				if (!sched.isShutdown()) {
					sched.shutdown();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
